package ninja.awesome.game.gameobject;

import java.util.Random;

import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Image;
import ninja.awesome.game.MainGameScreen;
import ninja.awesome.game.NinjaGame;
import ninja.awesome.game.compat.Rectangle;
import ninja.awesome.game.main.Animation;
import ninja.awesome.game.main.Controller;
import ninja.awesome.game.main.Sprites;

public class Building extends GameObj {

	private Image[] building;
	private int speed = 5;

	private int ticks = 0;
	private int timer = 0;

	private int imageNumber = 1;
	private double imageWidth = 0;
	private double imageHeight = 0;
	private int whatBuilding = 0;
	private int left = 0;
	private int right = 1;

	private boolean isUsed = false;

	private String direction = "left";
	Animation anim;
	Random r = new Random();
	MainGameScreen game;
	Sprites s;
	Controller c;

	public Building(int x, int y, MainGameScreen game, Controller c, Sprites s) {

		super(x, y);
		this.s = s;
		this.c = c;
		this.game = game;
		whatBuilding = r.nextInt(2);
		building = s.getBuilding(whatBuilding);

		if (whatBuilding == 0) { // two storey building
			this.imageWidth = 400;
			this.imageHeight = 441;

		} else if (whatBuilding == 1) { // one storey building

			this.imageWidth = 400;
			this.imageHeight = 283;
			yLoc = yLoc + 158;

		}

		anim = new Animation(0, building[0], imageHeight, imageWidth,
				imageNumber);

	}

	public void update() {

		// building movement when a player press movement key: Jung Hwan Kim
		if (game.getBuilding_Block()){
			this.right = 0; // block building movement!
		}else
			right  =1;
		if (game.getNinja().getDirection().equals("left")) {
			xLoc -= game.getNinja().getSpeedX()*1;
			if (xLoc > 0) {
				if (c.getBuilding().size() < 2)
					c.spawnBuilding(-1);
				else if (xLoc > NinjaGame.WIDTH)
					c.removeBuilding(this);
			}
		} else if (game.getNinja().getDirection().equals("right")) {
			xLoc -= game.getNinja().getSpeedX()*right;
			if (xLoc < 0) {
				if (c.getBuilding().size() < 2)
					c.spawnBuilding(1);
				else if (xLoc < -NinjaGame.WIDTH)
					c.removeBuilding(this);
			}

		}
		if (this.getIsUsed()){
			anim.setTempNumber(1);
		}

	}

	public void draw(Graphics g) {
		anim.drawAnimation(g, xLoc, yLoc);

	}

	public Rectangle getBounds() {
		return new Rectangle(xLoc, yLoc, 800, 600);
	}

	public int getX() {
		return this.xLoc;
	}

	public int getY() {
		return this.yLoc;
	}

	public void setRandomValue(int whatBuilding) {
		this.whatBuilding = whatBuilding;
	}

	public int getRandomValue() {
		return whatBuilding;
	}
	public void setIsUsed(boolean isUsed){
		this.isUsed = isUsed;
	}
	public boolean getIsUsed(){
		return this.isUsed;
	}
}
